package com.cqc.androidzinglibrary01;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private Context context = this;
    private static final int REQUEST_CODE = 100;
    private static final int REQUEST_IMAGE = 101;

    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.listView)
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.types, android.R.layout.simple_list_item_1);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case 1:
                String text = et.getText().toString().trim();
                Bitmap image = CodeUtils.createImage(text, 300, 300, null);
                iv.setImageBitmap(image);
                break;
            case 2:
                String text2 = et.getText().toString().trim();
                Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round);
                Bitmap image2 = CodeUtils.createImage(text2, 300, 300, logo);
                iv.setImageBitmap(image2);
                break;
            case 3:
                Intent intent_1 = new Intent(Intent.ACTION_GET_CONTENT);
                intent_1.addCategory(intent_1.CATEGORY_OPENABLE);
                intent_1.setType("image/*");
                startActivityForResult(intent_1, REQUEST_IMAGE);
                break;
            case 4:
                Intent intent_4 = new Intent(MainActivity.this, CustomScanActivity.class);
                startActivityForResult(intent_4, REQUEST_CODE);
                break;
            default:
                Toast.makeText(context, "click default", Toast.LENGTH_SHORT).show();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE:
                scanQR(data);
                break;
            case REQUEST_IMAGE:
                scanImage(data);
                break;
            default:
                Toast.makeText(context, "default", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void scanImage(Intent data) {
        if (data != null) {
            Uri uri = data.getData();
            String imagePath = ImageUtil.getImageAbsolutePath(context, uri);
            ContentResolver cr = getContentResolver();
            try {
                Bitmap mBitmap = MediaStore.Images.Media.getBitmap(cr, uri);//显得到bitmap图片

                CodeUtils.analyzeBitmap(imagePath, new CodeUtils.AnalyzeCallback() {
                    @Override
                    public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                        Toast.makeText(MainActivity.this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onAnalyzeFailed() {
                        Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                    }
                });

                if (mBitmap != null) {
                    mBitmap.recycle();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //    private String getImagePath(Uri uri, String selection) {
    //        String path = null;
    //        Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
    //        if (cursor != null) {
    //            if (cursor.moveToFirst()) {
    //                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    //            }
    //            cursor.close();
    //        }
    //        return path;
    //    }


    private void scanQR(Intent data) {
        if (data != null) {
            Bundle bundle = data.getExtras();
            if (bundle != null) {
                int resultType = bundle.getInt(CodeUtils.RESULT_TYPE);
                if (resultType == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
                } else if (resultType == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(context, "扫描失败", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
